import { User } from "@/api/interface";
import { customForm } from "@/components/ProForm/interface";
import { customTable } from "@/components/ProTable/interface";
import { ProTableValue } from "@/interface/public";

import { openForm } from "@/utils/form";

export function useDataGrid(
  proTableBox: ProTableValue,
  addForm: customForm<User.ResUserList>,
  changePwdForm: customForm<User.ResUserList>
) {
  let dataGrid = reactive<customTable>({
    // 表格配置项
    columns: [
      { prop: "operation", label: "operation", fixed: "left", width: 270 },
      {
        prop: "username",
        label: "username",
        search: {
          el: "input",
          key: "key",
          label: "关键值",
          props: {
            placeholder: "userPermissions_key"
          }
        }
      },
      {
        prop: "name",
        label: "name"
      },
      {
        prop: "status",
        label: "用户状态",
        tag: true,
        enum: status1()
      },
      {
        prop: "remark",
        label: "remark"
      }
    ],
    headButtonList: [
      {
        label: "addNewUsers",
        type: "primary",
        icon: "el-Plus",
        methods: () => {
          openForm<User.ResUserList>(addForm, "addNewUsers");
        }
      }
    ],
    rowButtonList: [
      {
        label: "edit",
        type: "primary",
        methods: (row: User.ResUserList) => openForm<User.ResUserList>(addForm, "editUsers", row),
        attrs: {
          link: true
        }
      },
      {
        label: "changePassword",
        type: "primary",
        methods: (row: User.ResUserList) => {
          const { id, username } = row;
          const params = {
            username,
            id
          };
          openForm<User.ResUserList>(changePwdForm, "changePassword", params);
        },
        attrs: {
          link: true
        }
      },
      {
        label: "enable",
        type: "primary",
        hide: item => item.status === 1,
        methods: (row: User.ResUserList) => {
          const params = { id: row.id, status: 0 };
          useHandleData(
            userUpStatus,
            params,
            `${transitionLanguages("table", "disabled")}【${row.username}】${transitionLanguages("table", "user")}`,
            proTableBox.value?.getTableList
          );
        },
        attrs: {
          link: true
        }
      },
      {
        label: "disabled",
        type: "danger",
        hide: item => item.status === 0,
        methods: (row: User.ResUserList) => {
          const params = { id: row.id, status: 1 };
          useHandleData(
            userUpStatus,
            params,
            `${transitionLanguages("table", "disabled")}【${row.username}】${transitionLanguages("table", "user")}`,
            proTableBox.value?.getTableList
          );
        },
        attrs: {
          link: true
        }
      }
    ]
  });

  return { dataGrid };
}
